"""
反转一个单链表，并返回反转后的头节点。
"""
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def reverse_linked_list(head):
    prev = None  # 前驱节点，初始为None 过程中肯定需要这个变量来记录、维系某一个节点的前序节点的
    current = head  # 当前节点，初始为头节点
    # 遍历链表
    while current:
        next_node = current.next  # 保存当前节点的下一个节点
        current.next = prev  # 反转当前节点的指针，指向前驱节点
        prev = current  # 移动前驱节点指针到当前节点
        current = next_node  # 移动当前节点指针到下一个节点
    # 遍历结束后，prev成为新的头节点
    return prev


#根据参数 创建链表
def create_linked_list(arr):
    if not arr:
        return None
    head = ListNode(arr[0])
    current = head
    for val in arr[1:]:
        current.next = ListNode(val)
        current = current.next
    return head


def traverse_linked_list(head):
    result = []
    current = head
    while current:
        result.append(current.val)
        current = current.next
    return result


# 测试
head = create_linked_list([1, 2, 3, 4, 5])
reversed_head = reverse_linked_list(head)
print(traverse_linked_list(reversed_head))  # 输出: [5, 4, 3, 2, 1]


x = 0.1 + 0.2
y = 0.3
print(x,y,x == y)